home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-04-29 | 10.5 KB | 387 lines | [TEXT/MPS ] |
- /*
- * UMenuedWindow.cp
- *
- * Copyright 1991 Imagenetics
- * All rights reserved
- *
- * 19 Aug 91 EMB Created file
- * 22 Aug 91 EMB Modified design to be standalone unit
- * 1 Oct 91 LPG Changed Str255 to CStr255 (MacApp 3.0ß2)
- * Catch empty strings in AddToWindowMenu
- *
- */
-
- #ifndef __UMENUEDWINDOW__
- #include "UMenuedWindow.h"
- #endif
-
- class TMenuedWindowBehavior : public TBehavior
- {
- private:
- TList* fWindowsMenuData;
-
- short fMenuID;
- short fFirstItem;
- public:
-
- // Construction/Destruction
- virtual pascal void Free(); // OVERRIDE
- virtual pascal void IMenuedWindowBehavior(short menuID);
- virtual pascal void Initialize(); // OVERRIDE
-
- // Menu Commands
- virtual pascal void DoMenuCommand(CommandNumber aCommand); // OVERRIDE
- virtual pascal void DoSetupMenus(); // OVERRIDE
-
- // Window menu
- virtual pascal void AddToWindowMenu(TWindow* aWindow);
- virtual pascal void DeleteFromWindowMenu(TWindow* aWindow);
- virtual pascal TWindow* MenuItemToWindow(short menuItem);
- virtual pascal void SelectWindowMenuWindow(short itemNo);
- virtual pascal short WindowToMenuItem(TWindow* aWindow);
- };
-
-
- /*********************************************************************************************
- U M e n u e d W i n d o w G l o b a l R o u t i n e s
- *********************************************************************************************/
-
- TMenuedWindowBehavior* gMenuedWindowBehavior = NULL;
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AInit
- pascal void InitUMenuedWindow(ResNumber menuID)
- {
- gMenuedWindowBehavior = NULL;
-
- if (gDeadStripSuppression)
- {
- macroDontDeadStrip(TFloatMenuedWindow);
- macroDontDeadStrip(TMenuedWindow);
- }
-
- if (gApplication == NULL)
- {
- if (qDebug)
- ProgramBreak("InitUMenuedWindow must be called in or after your IMyApplication method");
- Failure(minErr, 0);
- }
-
- gMenuedWindowBehavior = new TMenuedWindowBehavior;
- gMenuedWindowBehavior->IMenuedWindowBehavior(menuID);
- gApplication->AddBehavior(gMenuedWindowBehavior);
- }
-
- //--------------------------------------------------------------------------------------------------
- /*
- * In the spirit of SetCommandName and SetCommandIcon we have another utility
- * routine to handle the menu item style
- */
- #pragma segment MAUtilitiesRes
- pascal void SetCommandStyle(CommandNumber aCommand, short chStyle)
- {
- ResNumber menu;
- short item;
- MenuHandle aMenuHandle;
-
- aMenuHandle = CommandToComponents(aCommand, menu, item);
- if (aMenuHandle)
- SetItemStyle(aMenuHandle, item, chStyle);
- }
-
-
- /*********************************************************************************************
- T M e n u e d W i n d o w C o H a n d l e r
- *********************************************************************************************/
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TMenuedWindowBehavior::AddToWindowMenu(TWindow* aWindow)
- {
- CStr255 itsTitle;
-
- aWindow->GetTitle(itsTitle);
-
- if (itsTitle.IsEmpty())
- itsTitle = " "; // AppendMenu doesn't like empty strings.
-
- AppendMenu(MAGetMenu(fMenuID), itsTitle);
-
- fWindowsMenuData->InsertLast(aWindow);
- }
-
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TMenuedWindowBehavior::DoMenuCommand(CommandNumber aCommand) // OVERRIDE
- {
- short itsMenuID;
- short itsMenuItem;
-
- if (aCommand < 0)
- {
- CommandToMenuItem(aCommand, itsMenuID, itsMenuItem);
- if (itsMenuID == fMenuID)
- this->SelectWindowMenuWindow(itsMenuItem);
- else
- inherited::DoMenuCommand(aCommand);
- }
- else
- inherited::DoMenuCommand(aCommand);
- }
-
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TMenuedWindowBehavior::DoSetupMenus() // OVERRIDE
- {
- CommandNumber itsCommandNumber;
- TWindow* itsWindow = NULL;
- short frontMenuItem;
- CStr255 itsTitle;
- short itsMenuItem;
- short i;
-
- inherited::DoSetupMenus();
-
- // Enable the variant portion of the windows menu
- frontMenuItem = this->WindowToMenuItem(gApplication->GetActiveWindow());
- for (i = short(fWindowsMenuData->GetSize()); i > 0; i--)
- {
- itsMenuItem = i + (fFirstItem - 1);
- itsCommandNumber = CommandFromMenuItem(fMenuID, itsMenuItem);
- itsWindow = this->MenuItemToWindow(itsMenuItem);
-
- // Enable the item, checking if it is the front item
- EnableCheck(itsCommandNumber, TRUE, itsMenuItem == frontMenuItem);
-
- // Make sure the name is up-to-date
- itsWindow->GetTitle(itsTitle);
- SetCommandName(itsCommandNumber, itsTitle);
-
- // Make the item style plain if it is visible, italics if hidden
- if (itsWindow->IsShown())
- SetCommandStyle(itsCommandNumber, normal);
- else
- SetCommandStyle(itsCommandNumber, italic);
-
- itsWindow = NULL;
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TMenuedWindowBehavior::DeleteFromWindowMenu(TWindow* aWindow)
- {
- short itsMenuItem;
-
- if (aWindow != NULL)
- {
- itsMenuItem = this->WindowToMenuItem(aWindow);
-
- if (itsMenuItem != kEmptyIndex)
- DelMenuItem(MAGetMenu(fMenuID), itsMenuItem);
-
- fWindowsMenuData->Delete(aWindow);
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ATerminate
- pascal void TMenuedWindowBehavior::Free() // OVERRIDE
- {
- fWindowsMenuData = (TList*)FreeIfObject(fWindowsMenuData);
-
- inherited::Free();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AInit
- pascal void TMenuedWindowBehavior::IMenuedWindowBehavior(short menuID)
- {
- FailInfo fi;
-
- this->IBehavior(kMenuedWindowBehavior);
-
- if (fi.Try())
- {
- fWindowsMenuData = NewList();
- if (qDebug)
- fWindowsMenuData->SetEltType("TWindow");
-
- fMenuID = menuID;
- fFirstItem = CountMItems(MAGetMenu(menuID)) + 1;
-
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AInit
- pascal void TMenuedWindowBehavior::Initialize() // OVERRIDE
- {
- inherited::Initialize();
-
- fWindowsMenuData = NULL;
-
- fMenuID = 0;
- fFirstItem = 0;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ARes
- pascal TWindow* TMenuedWindowBehavior::MenuItemToWindow(short menuItem)
- {
- TWindow* result = NULL;
- ArrayIndex windowIndex;
-
- if (menuItem > 0)
- {
- windowIndex = menuItem - (fFirstItem - 1);
- if (windowIndex > kEmptyIndex && windowIndex <= fWindowsMenuData->GetSize())
- result = (TWindow*)fWindowsMenuData->At(windowIndex);
- }
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TMenuedWindowBehavior::SelectWindowMenuWindow(short itemNo)
- {
- TWindow* itsWindow;
-
- if (itemNo > 0)
- {
- itsWindow = this->MenuItemToWindow(itemNo);
- if (itsWindow != NULL)
- {
- if (!itsWindow->IsShown())
- itsWindow->Open();
- itsWindow->Select();
- }
- else if (qDebug)
- ProgramBreak("Problem: I’ve been asked to select a Windows menu window that doesn’t exist");
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment ARes
- pascal short TMenuedWindowBehavior::WindowToMenuItem(TWindow* aWindow)
- {
- short result = 0;
- short windowIndex;
-
- if (aWindow != NULL)
- {
- // The fourth item is the first one placed in the windows menu (and hence
- // tracked in fWindowsMenuData.
- windowIndex = short(fWindowsMenuData->GetIdentityItemNo(aWindow));
- if (windowIndex != kEmptyIndex)
- result = windowIndex + (fFirstItem - 1);
- }
-
- return result;
- }
-
- /*********************************************************************************************
- T F l o a t M e n u e d W i n d o w
- *********************************************************************************************/
- //--------------------------------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TFloatMenuedWindow::DoPostCreate(TDocument* itsDocument) // OVERRIDE
- {
- inherited::DoPostCreate(itsDocument);
-
- if (gMenuedWindowBehavior != NULL)
- gMenuedWindowBehavior->AddToWindowMenu(this);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TFloatMenuedWindow::Free() // OVERRIDE
- {
- if (gMenuedWindowBehavior != NULL)
- gMenuedWindowBehavior->DeleteFromWindowMenu(this);
-
- inherited::Free();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TFloatMenuedWindow::IFloatMenuedWindow(TDocument* itsDocument,
- WindowPtr itsWMgrWindow, Boolean canResize,
- Boolean canClose, Boolean disposeOnFree)
- {
-
- this->IFloatWindow(itsDocument, itsWMgrWindow, canResize, canClose, disposeOnFree);
-
- FailInfo fi;
- if (fi.Try())
- {
- if (gMenuedWindowBehavior != NULL)
- gMenuedWindowBehavior->AddToWindowMenu(this);
-
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- }
-
- /*********************************************************************************************
- T M e n u e d W i n d o w
- *********************************************************************************************/
- //--------------------------------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TMenuedWindow::DoPostCreate(TDocument* itsDocument) // OVERRIDE
- {
- inherited::DoPostCreate(itsDocument);
-
- if (gMenuedWindowBehavior != NULL)
- gMenuedWindowBehavior->AddToWindowMenu(this);
- }
-
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TMenuedWindow::Free() // OVERRIDE
- {
- if (gMenuedWindowBehavior != NULL)
- gMenuedWindowBehavior->DeleteFromWindowMenu(this);
-
- inherited::Free();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TMenuedWindow::IMenuedWindow(TDocument* itsDocument, WindowPtr itsWMgrWindow,
- Boolean canResize, Boolean canClose,
- Boolean disposeOnFree)
- {
-
- this->IWindow(itsDocument, itsWMgrWindow, canResize, canClose, disposeOnFree);
-
- FailInfo fi;
- if (fi.Try())
- {
- if (gMenuedWindowBehavior != NULL)
- gMenuedWindowBehavior->AddToWindowMenu(this);
-
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- }
-
-